package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type Article struct {
	Id           int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	Type         byte      `gorm:"column:type"`
	ThumbnailUrl string    `gorm:"column:thumbnail_url"`
	VideoPic     string    `gorm:"column:video_pic"`
	VideoKey     string    `gorm:"column:video_key"`
	WatchCount   int64     `gorm:"column:watch_count"`
	LikeCount    int64     `gorm:"column:like_count"`
	Description  string    `gorm:"column:description"`
	Title        string    `gorm:"column:title"`
	Content      string    `gorm:"column:content"`
	CreateAt     time.Time `gorm:"column:create_at"`
	UpdateAt     time.Time `gorm:"column:update_at"`
}

func (a *Article) TableName() string {
	return "article"
}

type ArticleE struct{}

func (a *ArticleE) TableName() string {
	return "article"
}

func (a *ArticleE) SelectAllByIdType(id []int64, Type byte) (articles []Article0, err error) {
	err = nspel.GetOrm().Select([]string{"id", "thumbnail_url", "video_pic", "video_key", "watch_count", "like_count", "description", "title", "create_at"}).Where("type = ?", Type).Where("id in (?)", id).Find(&articles).Error
	return
}

func (a *ArticleE) SelectAllByTypeOrderByCreateAtDesc(Type byte) (articles []Article0, err error) {
	key := "article0.type:" + strconv.Itoa(int(Type)) + ".create_at.desc.all"
	err = nspel.RedisGetGob(key, &articles)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "thumbnail_url", "video_pic", "video_key", "watch_count", "like_count", "description", "title", "create_at"}).Where("type = ?", Type).Order("create_at desc").Find(&articles).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, articles)
	return
}

func (a *ArticleE) SelectAllByTypeOrderByWatchCountDesc(Type byte) (articles []Article0, err error) {
	key := "article0.type:" + strconv.Itoa(int(Type)) + ".watch_count.desc.all"
	err = nspel.RedisGetGob(key, &articles)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "thumbnail_url", "video_pic", "video_key", "watch_count", "like_count", "description", "title", "create_at"}).Where("type = ?", Type).Order("watch_count desc").Find(&articles).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, articles)
	return
}

func (a *ArticleE) SelectCountByType(Type byte) (count int64, err error) {
	key := "article.type:" + strconv.Itoa(int(Type)) + ".count"
	err = nspel.RedisGetInt64(key, &count)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Table(a.TableName()).Where("type = ?", Type).Count(&count).Error; err != nil {
		return
	}
	nspel.RedisSetInt64(key, count)
	return
}

func (a *ArticleE) UpdateWatchCountAddById(id int64) (watchCount int64, err error) {
	err = nspel.GetOrm().Exec("UPDATE article SET watch_count = watch_count + 1 WHERE id = ?", id).Error
	if err != nil {
		return
	}
	article2 := Article2{}
	err = article2.SelectById(id)
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("article0.type:"+strconv.Itoa(int(article2.Type))+".create_at.desc.all", "article0.type:"+strconv.Itoa(int(article2.Type))+".watch_count.desc.all", "article1.id:"+strconv.FormatInt(id, 10), "article3.id:"+strconv.FormatInt(id, 10))
	article3 := Article3{}
	err = article3.SelectById(id)
	if err != nil {
		return
	}
	watchCount = article3.WatchCount
	return
}

type Article0 struct {
	Id           int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	ThumbnailUrl string    `gorm:"column:thumbnail_url"`
	VideoPic     string    `gorm:"column:video_pic"`
	VideoKey     string    `gorm:"column:video_key"`
	WatchCount   int64     `gorm:"column:watch_count"`
	LikeCount    int64     `gorm:"column:like_count"`
	Description  string    `gorm:"column:description"`
	Title        string    `gorm:"column:title"`
	CreateAt     time.Time `gorm:"column:create_at"`
}

func (a *Article0) TableName() string {
	return "article"
}

type Article1 struct {
	Id           int64     `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	Type         byte      `gorm:"column:type"`
	ThumbnailUrl string    `gorm:"column:thumbnail_url"`
	VideoPic     string    `gorm:"column:video_pic"`
	VideoKey     string    `gorm:"column:video_key"`
	WatchCount   int64     `gorm:"column:watch_count"`
	LikeCount    int64     `gorm:"column:like_count"`
	Description  string    `gorm:"column:description"`
	Title        string    `gorm:"column:title"`
	Content      string    `gorm:"column:content"`
	CreateAt     time.Time `gorm:"column:create_at"`
}

func (a *Article1) TableName() string {
	return "article"
}

func (a *Article1) SelectById(id int64) (err error) {
	key := "article1.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, a)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "type", "thumbnail_url", "video_pic", "video_key", "watch_count", "like_count", "description", "title", "content", "create_at"}).Where("id = ?", id).First(a).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, a)
	return
}

type Article2 struct {
	Type byte `gorm:"column:type"`
}

func (a *Article2) TableName() string {
	return "article"
}

func (a *Article2) SelectById(id int64) (err error) {
	key := "article2.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, a)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("type").Where("id = ?", id).First(a).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, a)
	return
}

type Article3 struct {
	WatchCount int64 `gorm:"column:watch_count"`
}

func (a *Article3) TableName() string {
	return "article"
}

func (a *Article3) SelectById(id int64) (err error) {
	key := "article3.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, a)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("watch_count").Where("id = ?", id).First(a).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, a)
	return
}
